#include "mid_Autochip.h"
#include "canIf.h"
#include "canIf_Cfg.h"
#include "app_can.h"
#include "app_main.h"
/* Private variables ----------------------------------------------------------*/
can_state_t	can0_State;

struct_CanCtrl_t g_can_ctrl;
/* Private functions ---------------------------------------------------------*/


/*FUNCTION**********************************************************************
 *
 * Function Name : can_pdu_disable_communication
 * Description   : Disable the APP PDU transmission ability due to a ISO14229 Communication
 *                 Control (28hex) service
 *
 *END**************************************************************************/
Enum_Result_t CanUser_DisableCommunication( void )
{
	can_state_t *canState = &can0_State;

	canState->CommunicationEnabled = FALSE;
	return E_OK;
}
Enum_Result_t CanUser_EnableCommunication( void )
{
	can_state_t *canState = &can0_State;

	canState->CommunicationEnabled = TRUE;
	return E_OK;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : can_car_infobuf_init
 * Description   : the function is used to init buf .
 *
 *END**************************************************************************/
static void can_car_infobuf_init( void )
{
	Can_CanBusOffParaInit0();
}
/*FUNCTION**********************************************************************
 *
 * Function Name : can_Init
 * Description   : Initialize the complete mscan module
 *
 *END**************************************************************************/
void can_Init(uint8_t instance)
{
	if(instance == CAN0CH)
	{
		CanIf_Init(&CanIf_Config);
		can0_State.workState = CAN_OPERATING_UNINIT;
//		mid_can_Init(CAN0CH, &can0_State, &can0_InitConfig);
		mid_can_Init(CAN0CH);
		mid_CanWakeUp(CAN0CH);
		can_car_infobuf_init();
	}
	else
	{
		
	}
}


/*FUNCTION**********************************************************************
 *
 * Function Name : can_1msTimer
 * Description   :
 * 
 *END**************************************************************************/
void can_1msTimer(void)
{
	Can_CanBusOffTimeCnt();
	mid_canTimeout1ms(INST_CAN0COM);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : can_10msTimer
 * Description   :
 * 
 *END**************************************************************************/
void can_10msTimer(void)
{
	g_can_ctrl.u8Timer10msFlag = TRUE;
}


/*FUNCTION**********************************************************************
 *
 * Function Name : can_sysevent_handler
 * Description   :
 * 
 *END**************************************************************************/
void can_sysevent_handler(uint8_t *p_event)
{
	SysEvent_Define * evt = (SysEvent_Define*)p_event;
	
//	if (module_src != EModule_can)
//		return;
	
	switch(evt->type)
	{
		case SysEvent_Null:
			break;

		case SysEvent_Reset:
			g_can_ctrl.enWorkState = CAN_SLEEP;
			break;
			
		case SysEvent_PowOn:
			can_Init(CAN0CH);
			g_can_ctrl.enWorkState = CAN_WORKING;
			break;

		case SysEvent_PowDown:
			can_car_infobuf_init();
			g_can_ctrl.enWorkState = CAN_SLEEP;
			break;

		case SysEvent_ACC:
			break;
			
		case SysEvent_ChangePowState:
			break;
			
		default:
			break;
	}
}


/*FUNCTION**********************************************************************
 *
 * Function Name : can_sysevent_handler
 * Description   :
 * 
 *END**************************************************************************/
uint32_t can_message_handler(uint8_t *p_message)
{
	return E_NOSPT;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : can_service
 * Description   :
 * 
 *END**************************************************************************/
void can_service(void)
{
	if(g_can_ctrl.u8Timer10msFlag == TRUE)
	{
		g_can_ctrl.u8Timer10msFlag = FALSE;
	}
	if(can0_State.workState == CAN_OPERATING_WORKING)
	{
		if(CANBusState(CAN0CH) == CAN_NORMAL)
		{
			mid_canTxService(CAN0CH);
		}
	}
	if(g_can_ctrl.enWorkState == CAN_WORKING)
	{
		mid_can_busOffProc10ms(CAN0CH);
	}
	mid_canRxIndication(CAN0CH);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : can_is_sleep
 * Description   :
 * 
 *END**************************************************************************/
uint32_t can_is_sleep(void)
{
	uint32_t ret=1;
	return ret;
}


